<!--
   	 作者：陈泽椿
   	 时间：2018-08-23
   	 描述：导入解析Excel表文件
   	 
  			v-import前面若是有其他控件，需要用<div style="float: left;">包裹起来
  			
  	参数: 	colnameData[]参数----约定的列名列表
  			colName[]------------数据库字段名列表
  			cmd  ''--------------调用的cmd命令名
  	回调函数:
  			@importResult--------------返回导入结果

        说明：在组件中用v-if="showInput"	,在回调函数里面用
        this.showInput = false;
		this.$nextTick(function(){
		    this.showInput = true;
		})可以避免第二次上传相同文件无法响应change事件的问题
-->

<template>
	<div >
		<div class="content-a-upload">
			<input type="file" id="file-select" @change="handleChange">Excel表导入
		</div>
	</div>
</template>

<script>
	import { query, apiparas, execute } from '../../api/api';
	export default {
		props: ['colnameData','colName','cmd','type'],
		data() {
			return {
				xlsData: {},
				dataHead: [{"name":"dd","value":"dd"}],
				paras:{}
			}
		},
		mounted: function() {
			//生命周期
			
		},
		computed: {
			//计算属性
			
		},
		methods: { //函数
			handleChange(event){
				//console.log(BASE_API);
				if(event.target.files.length===0||event===null){
					return;
				}
				let _this = this;
				console.log(event,"我是event")
				// 通过DOM取文件数据
				let file = event.target.files[0];
				console.log(file,"w shi file")
				//return;
				let rABS = false; //是否将文件读取为二进制字符串
				let f = file;
				let reader = new FileReader();
				//if (!FileReader.prototype.readAsBinaryString) {
				FileReader.prototype.readAsBinaryString = function(f) {
					let binary = "";
					let rABS = false; //是否将文件读取为二进制字符串
					let pt = this;
					let wb; //读取完成的数据
					console.log(wb,"我是wb")
					let outdata;
					let reader = new FileReader();
					
					reader.onload = function(e) {
						try{
							let bytes = new Uint8Array(reader.result);
							let length = bytes.byteLength;
							for(let i = 0; i < length; i++) {
								binary += String.fromCharCode(bytes[i]);
							}
							let XLSX = require('xlsx');
							if(rABS) {
								wb = XLSX.read(btoa(fixdata(binary)), { //手动转化
									type: 'base64'
								});
							} else {
								wb = XLSX.read(binary, {
									type: 'binary'
								});
							}
	
							outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]], {
								header: 1
							}); //outdata就是你想要的东西
							_this.xlsData = [...outdata];
							console.log(_this.xlsData,'_this.xlsData');
							
							
							if( _this.xlsData[1][0] === undefined ){
								_this.$notify.error({title: '错误',message:'请填写店铺编码' ,type:'error'});
								return
							}
							if( _this.xlsData[1][2] === undefined ){
								_this.$notify.error({title: '错误',message:'请填写年份' ,type:'error'});
								return
							}
							
							let entity_code = _this.xlsData[1][0];
							let year = _this.xlsData[1][2];
							console.log(entity_code,'entity_code')
							console.log(year,'year')
							let cost_value = '';
							for(let i = 3,len = 15; i<len; i++){
								cost_value = cost_value + '[';
								if(_this.xlsData[i][2] !== undefined){
									_this.xlsData[i][2] = Number( _this.xlsData[i][2] )
								}
								
								if( _this.xlsData[i][1] !== undefined && _this.xlsData[i][2] !== undefined){
									_this.xlsData[i][2] = undefined;
								}

								// for(let j = 0,len = _this.xlsData[i].length;j<len; j++){
								for(let j = 0,len = _this.xlsData[i].length===1?1:7; j<len; j++){
									if( _this.xlsData[i][j] === undefined ){
										cost_value = cost_value + '0' + ','
									}else{
										cost_value = cost_value + _this.xlsData[i][j] + ','
									}
								}
								cost_value = cost_value.substring(0,cost_value.length-1);
								cost_value = cost_value +  '],'
							}
							cost_value = cost_value.substring(0,cost_value.length-1);
							let jsonparas = [
								{'name':'cost_value','value':cost_value},
								{'name':'entity_code','value':entity_code},
								{'name':'year','value':year},
							];
							let _paras = apiparas.getParas('default',_this.cmd,jsonparas);
							console.log(_paras,'paras');
							execute(_paras).then(response => {
								console.log(response,'response')
								if(response.errid == 0){
									_this.$notify({title: '成功信息',message: '导入成功！',type: 'success'});
									_this.$emit('importResult',true);
								}
								else
								{
									_this.$emit('importResult',false);
									_this.$notify.error({title: '错误',message:response.errmsg ,type:'error'});
								}
								event = null;
							});	
						}catch(e){
							_this.$message({
								message: '导入失败！请导入Excel文件',
								type: 'error'
							});
						}
					}
					reader.readAsArrayBuffer(f);
				}
				if(rABS) {
					reader.readAsArrayBuffer(f);
				} else {
					reader.readAsBinaryString(f);
				}
			}
		},
		created: function() {
			
		},
		update:function(){
			
		},
		watch: {
		
		}

	}
</script>

<style>
.content-a-upload {
    position: relative;
    top:0px;
    height: 39px;
    /*width: 50px;*/
   	font: 10 15px/40px "宋体";
    text-align: center;
    cursor: pointer;
    color: #fff;
    background: #409EFF;
    border: 1px solid #b3d8ff;
    border-radius: 5px;
    overflow: hidden;
    display: inline-block;
    text-decoration: none;
}
.content-a-upload input {
    position: absolute;
    font-size: 15px;
    height: 40px;
    right: 0;
    top: 0px;
    opacity: 0;
    cursor: pointer;
}
.content-a-upload:hover {
    color: #fff;
    background: #409EFF;
    border-color: #409EFF;
    text-decoration: none
}
</style>